package com.itzhihao.controller;

import com.itzhihao.common.constant.OrderStatus;
import com.itzhihao.entity.Order;
import com.itzhihao.entity.Product;
import com.itzhihao.entity.Store;
import com.itzhihao.service.IOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Resource
    private IOrderService orderService;

    /**
     * 查询不同状态下的多订单
     * @param pageDomain
     * @param order
     * @return
     */
    @PostMapping("/list")
    public TableDataInfo getList(@RequestBody PageDomain pageDomain,
                                 @RequestBody Order order){
        return orderService.getList(pageDomain,order);
    }

    /**
     * 通过商店Id查询多订单
     * @param pageDomain
     * @param store
     * @return
     */
    @PostMapping("/selectListByStoreId")
    public TableDataInfo selectListByStoreId(@RequestBody PageDomain pageDomain,
                                             @RequestBody Store store){
        return orderService.selectListByStoreId(pageDomain,store);
    }

    /**
     * 根据商品id查询多个订单
     * @param pageDomain
     * @param product
     * @return
     */
    @PostMapping("/selectListByProductId")
    public TableDataInfo selectListByProductId(@RequestBody PageDomain pageDomain,
                                               @RequestBody Product product){
        return orderService.selectListByProductId(pageDomain,product);
    }

    /**
     * 查看订单详情
     * @param id
     * @return
     */
    @GetMapping
    public AjaxResult getDetail(@RequestParam(value = "order_id") Long id){
        Order order = orderService.getDetail(id);
        return success(order);
    }

    @PutMapping
    public AjaxResult update(@RequestBody Order order){
        boolean isUpdate = orderService.updateById(order);
        if(isUpdate){
            return AjaxResult.success("更新成功");
        }else{
            return AjaxResult.error("更新失败，请重试");
        }
    }

    @PutMapping("/sended")
    public AjaxResult updateSended(@RequestBody Order order){
        boolean isUpdate = orderService.updateStatus(order, OrderStatus.SENDED.ordinal());
        if(isUpdate){
            return AjaxResult.success("更新成功");
        }else{
            return AjaxResult.error("更新失败，请重试");
        }
    }
    @PutMapping("/arrived")
    public AjaxResult updateArrived(@RequestBody Order order){
        boolean isUpdate = orderService.updateStatus(order, OrderStatus.ARRIVED.ordinal());
        if(isUpdate){
            return AjaxResult.success("更新成功");
        }else{
            return AjaxResult.error("更新失败，请重试");
        }
    }
    @PutMapping("/got")
    public AjaxResult updateGot(@RequestBody Order order){
        boolean isUpdate = orderService.updateStatus(order, OrderStatus.GOT.ordinal());
        if(isUpdate){
            return AjaxResult.success("更新成功");
        }else{
            return AjaxResult.error("更新失败，请重试");
        }
    }
    @PutMapping("/cancel")
    public AjaxResult updateCancel(@RequestBody Order order){
        boolean isUpdate = orderService.updateStatus(order, OrderStatus.CANCEL.ordinal());
        if(isUpdate){
            return AjaxResult.success("更新成功");
        }else{
            return AjaxResult.error("更新失败，请重试");
        }
    }

    @DeleteMapping("/delete/{order_id}")
    public AjaxResult delete(@PathVariable(value = "order_id") Long id){
        boolean isDelete = orderService.deleteById(id);
        if(isDelete){
            return AjaxResult.success("删除成功");
        }else{
            return AjaxResult.error("删除失败，请重试");
        }
    }

    /**
     * 新增
     * @param order
     * @return
     */
    @PostMapping
    public AjaxResult add(@RequestBody Order order){
        boolean isSave = orderService.saveOrder(order);
        if (isSave){
            return AjaxResult.success("保存成功");
        }else{
            return AjaxResult.error("保存失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Delete("/delete/{ids}")
    public AjaxResult deleteBatch(@PathVariable String ids){
        Long[] idLongArr = Convert.toLongArray(ids);
        List<Long> idLongList = Arrays.asList(idLongArr);
        boolean isDelete = orderService.deleteByIds(idLongList);
        if(isDelete){
            return success("批量删除成功");
        }else{
            return error("批量删除失败");
        }
    }
}
